package Model;

import java.sql.ResultSet;
import java.sql.Statement;

public class Advertisement {

	
	   private DbConnection db = null;
	   private Statement st = null;
	   private ResultSet rs = null; 
	   
	   private int id_ad=-1;
	   private int id_owner=-1;
	   private String content=null;
	   private String title=null;
	   private String date_pub=null;
	   private String date_update=null;
	   private int id_car=-1;
	   private double price=-1.1;
	   private int distance = -1;
	   private String year = null;
	   private boolean crashed = false;
	   
	   public Advertisement(){
		   db = new DbConnection();	   
		   st = db.getStatement();
	   }
	   
	   public Advertisement(int id){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select * from advertisement "+
												" WHERE id_ad="+id+";");
				this.rs.next();
				this.id_ad = this.rs.getInt("id_ad");
				this.id_owner= this.rs.getInt("id_owner");
				this.content= this.rs.getString("content");
				this.title = this.rs.getString("title");
				this.date_pub = this.rs.getString("date_pub");
				this.date_update = this.rs.getString("date_update");
				this.id_car = this.rs.getInt("id_car");
				this.price = this.rs.getDouble("price");
				this.distance = this.rs.getInt("distance");
				this.year = this.rs.getString("year");
				this.crashed = this.rs.getBoolean("crashed");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   public int getIdAd(){
		   return this.id_ad;
	   }
	   
	   public int getIdOwner(){
		   return this.id_owner;
	   }
	   
	   public String getContent(){
		   return this.content;
	   }
	   
	   public String getTitle(){
		   return this.title;
	   }
	   
	   public String getDatePub(){
		   return this.date_pub;
	   }
	   
	   public String getDateUpdate(){
		   return this.date_update;
	   }
	   
	   public int getIdCar(){
		   return this.id_car;
	   }
	   
	   public double getPrice(){
		   return this.price;
	   }
	   
	   public int getDistance(){
		   return this.distance;
	   }
	   
	   public String getYear(){
		   return this.year;
	   }
	   
	   public boolean getCrashed(){
		   return this.crashed;
	   }
	   
	   
	   public void setAdveriement(int id){
		   try{
				this.rs = this.st.executeQuery("Select * from advertisement "+
												" WHERE id_ad="+id+";");
				this.rs.next();
				this.id_ad = this.rs.getInt("id_ad");
				this.id_owner= this.rs.getInt("id_owner");
				this.content= this.rs.getString("content");
				this.title = this.rs.getString("title");
				this.date_pub = this.rs.getString("date_pub");
				this.date_update = this.rs.getString("date_update");
				this.id_car = this.rs.getInt("id_car");
				this.price = this.rs.getDouble("price");
				this.distance = this.rs.getInt("distance");
				this.year = this.rs.getString("year");
				this.crashed = this.rs.getBoolean("crashed");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   
	   
	   
	   //METODY BAZODANOWE
	   
	   
	   public int insertAdvertisement(int id_owner,String content, String title,String date_pub,String date_update,int id_car, 
	            						double price,int distance,String year,boolean crashed){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("INSERT INTO advertisement( "+
									            "id_owner, \"content\", title, date_pub, date_update, id_car,"+ 
									            " distance, \"year\", crashed, price) "+
									           " VALUES ("+id_owner+", '"+content+"', '"+title+"', '"+date_pub+"', '"+date_update+"', "+id_car+", "+ 
									           " "+distance+", '"+year+"', "+crashed+", "+price+") returning id_ad;");
				
				
				
				this.rs.next();
				result = this.rs.getInt("id_ad");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}
	   
	   public int deleteAdvertisementById(int id){
		   
		   int result = -1;
			try{
				this.rs = this.st.executeQuery("DELETE FROM advertisement WHERE id_ad="+id+" returning id_ad");
				
				this.rs.next();
				result = this.rs.getInt("id_ad");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		   
	   }
	   
	   public int updateAdvertisement(int id_owner,String content, String title,String date_pub,String date_update,int id_car, 
				double price,int distance,String year,boolean crashed, int id_ad){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("UPDATE advertisement "+
							   "SET  id_owner="+id_owner+", \"content\"='"+content+"', title='"+title+"', date_pub='"+date_pub+"', date_update='"+date_update+"', "+ 
							       " id_car="+id_car+", distance="+distance+", \"year\"='"+year+"', crashed="+crashed+", price="+price+" "+
							 " WHERE id_ad="+id_ad+" returning id_ad ;");
				
				this.rs.next();
				result = this.rs.getInt("id_ad");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
	   }
	   
}
